<?php


namespace app\sys\service;


use OSS\Core\OssException;
use OSS\OssClient;
use think\facade\Env;
use think\facade\Log;

class AliOss
{
    private $bucket;
    private $ossClient;
    public function __construct()
    {
        $accessKeyId = Env::get("ALIOSS.ACCESS_ID");
        $accessKeySecret = Env::get("ALIOSS.ACCESS_KEY");
        $endpoint = Env::get("ALIOSS.OSS_ENDPOINT");
        $this->bucket =  Env::get("ALIOSS.OSS_BUCKET");
        try {
            $this->ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
        } catch (OssException $e) {
            Log::info("oss_upload_content".$e->getMessage());
        }
    }

    /**
     * 上传文件
     * @param $oss_object
     * @param $file_content
     * @param string $acl private|public-read
     * @return string|null
     */
    public function oss_upload_content($oss_name, $file, $acl = "private")
    {
        try {
            $contents = file_get_contents($file);
            $this->ossClient->putObject($this->bucket, $oss_name, $contents);
        } catch (OssException $e) {
            Log::info("oss_upload_content".$e->getMessage());
            return $e->getMessage();
        }
        return null;
    }

    //删除oss对象
    public function oss_delete_file($oss_object)
    {
        $this->ossClient->deleteObject($this->bucket, $oss_object);
        return null;
    }
}